package com.nikkerball.apps.darkhorse.model.base;

import com.nikkerball.apps.darkhorse.model.change.ChangeEvent;
import com.nikkerball.apps.darkhorse.model.change.ChangeValue;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * The Base Classroom of all Darkhorse model objects
 *
 * @author Marius
 */
@MappedSuperclass
public class TouchableObject extends BaseObject {

    // ----- instance fields -----

    private DateTime modifyDate;

    private String modifyUser;

    // ----- constructors -----

    @SuppressWarnings({"RedundantNoArgConstructor"})
    public TouchableObject() {
        super();
    }

    // ----- abstract methods -----

    @Transient
    public List<ChangeValue> getChangeValues(ChangeEvent changeEvent) {
        List<ChangeValue> changeValues = new ArrayList<ChangeValue>();
        return changeValues;
    }

    // ----- public methods -----

    @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
    @Column(nullable = false)
    public DateTime getModifyDate() {
        return this.modifyDate;
    }

    public void setModifyDate(DateTime modifyDate) {
        this.modifyDate = modifyDate;
    }

    @Column(length = USER_COLUMN_SIZE, nullable = false)
    public String getModifyUser() {
        return this.modifyUser;
    }

    public void setModifyUser(String modifyUser) {
        this.modifyUser = modifyUser;
    }

    @Transient
    public void touch(String byWho) {

        if (null == getCreateDate()) {
            setCreateDate(new DateTime());
        }
        if (null == getCreateUser()) {
            setCreateUser(byWho);
        }

        setModifyDate(new DateTime());
        setModifyUser(byWho);

    }
}
